<?php 
/***
    Amrita ITEWS - Copyright (C) 2009 Amrita Vishwa Vidyapeetham, Amritapuri.
                                     (http://www.amrita.edu)
    ***************************************************************************
    This file is part of the Amrita ITEWS distribution.
    Amrita ITEWS is free software; you can redistribute it and/or modify it 
    under the terms of the GNU General Public License (version 2) as published 
    by the Free Software Foundation AND MODIFIED BY the Amrita ITEWS exception.
    ***NOTE*** The exception to the GPL is included to allow you to distribute
    a combined work that includes Amrita ITEWS without being obliged to provide
    the source code for proprietary components outside of the Amrita ITEWS
    software. Amrita ITEWS is distributed in the hope that it will be useful, 
    but WITHOUT ANY WARRANTY; without even the implied warranty of 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
    Public License for more details. You should have received a copy of the GNU
    General Public License and the Amrita ITEWS license exception along with 
    Amrita ITEWS if not then it can be viewed here: 
    http://itews.amrita.ac.in/license.html.
    
    Documentation, latest information, license and contact details are at:
    http://itews.amrita.ac.in/
 
    Amrita ITEWS source code can be found at:
    http://code.google.com/p/itews"; 
    
    The initial code-base of Amrita ITEWS was developed by Amrita Vishwa 
    Vidyapeetham as part of the project titled,\"Intelligent & Interactive 
    Telematics using Emerging Wireless Technologies for Transport Systems 
    (ITEWS)\" sponsored by Technology Information Forecasting and Assessment 
    Council (TIFAC), India.
***/
?><?php
//- taken from http://www.plus2net.com/php_tutorial/gd-linegp.php and http://www.plus2net.com/php_tutorial/gd-line1.php
header ("Content-type: image/png");
require_once("./utils.php.inc");
require_once("./settings.php.inc");

?>
<?php
$sql = 'select rs. * from route_segments rs , persistent_data pd where pd.id = 1 AND pd.current_route_code = rs.route_schedule_code ORDER BY rs.point_seq_no ASC';
if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;//used to check display of remaining body
	echo "Some error occured!";
}
else{

	////$x_gap=40; // The gap between each point in y axis

	$x_max= 3*100; // Maximum width of the graph or horizontal axis
	$y_max= 3*100; // Maximum hight of the graph or vertical axis
	// Above two variables will be used to create a canvas of the image//


	$im = @ImageCreate ($x_max, $y_max)
	or die ("Cannot Initialize new GD image stream");
	$background_color = ImageColorAllocate ($im, 0, 0, 85);
	$text_color = ImageColorAllocate ($im, 255, 255, 255);

	$start_color = ImageColorAllocate ($im, 0, 255, 0);
	$end_color = ImageColorAllocate ($im, 255, 0, 0);
	$mid_color = ImageColorAllocate ($im, 255, 255, 0);
	$graph_color = ImageColorAllocate ($im,255,255,255);

	/****************************
	min(point_lat) 	max(point_lat) 	min(point_long) 	max(point_long)
	9.0933097996929 	9.0998068175776 	76.489594306856 	76.492832338563
	****************************/
	$sql="select astext(line_segment) as line_segment from route_segments rs , persistent_data pd where pd.id = 1 AND pd.current_route_code = rs.route_schedule_code ORDER BY rs.point_seq_no ASC";
	if ( ($ls_result = query_wrapper($sql, __FILE__, __LINE__))==null){
		$error_occured = true;//used to check display of remaining body
		echo "Some error occured!";
	}
	else{
		$first_time = true;
		while($ls=mysql_fetch_array($ls_result, MYSQL_ASSOC)){
			if (ereg ("(LINESTRING\()([0-9]*.[0-9]*) ([0-9]*.[0-9]*),([0-9]*.[0-9]*) ([0-9]*.[0-9]*)(\))", $ls["line_segment"], $regs)) {
			    //echo "$regs[5].$regs[4].$regs[3].$regs[2].$regs[1]";
				if ($first_time){
					$min_lat = doubleval($regs[5]);
					$max_lat = doubleval($regs[5]);
					$min_long = doubleval($regs[4]);
					$max_long = doubleval($regs[4]);
					$first_time = false;
				}
				else{
					if(doubleval($regs[5])<($min_lat))
						$min_lat = doubleval($regs[5]);
					if(doubleval($regs[5])>($max_lat))
						$max_lat = doubleval($regs[5]);
					if(doubleval($regs[4])<($min_long))
						$min_long = doubleval($regs[4]);
					if(doubleval($regs[4])>($max_long))
						$max_long = doubleval($regs[4]);
				}
			} 
			else {
			    echo "Data in incorrect format!";
			}
		}
	}
	
	//echo "$min_lat^$max_lat^$min_long^$max_long";
	
	//$x_delta = abs(76.492832338563 - 76.489594306856)/0.95;
	//$y_delta = abs(9.0998068175776 - 9.0933097996929)/0.95;
	$x_delta = abs($max_long - $min_long)/0.95;
	$y_delta = abs($max_lat - $min_lat)/0.95;

	imagesetthickness($im,5);
	$x1=0;
	$y1=0;
	$first_one="yes";
	while($nt=mysql_fetch_array($result)){

	//echo "$nt[month], $nt[sales]";
	//$x2=-(intval((($x_max) *(76.489594306856-$nt[point_long]))/$x_delta))   +   5; // Shifting in X axis
	//$y2= intval((($y_max) *(9.0998068175776-$nt[point_lat]))/$y_delta)   + 5; // Coordinate of Y axis
	$x2=-(intval((($x_max) *($min_long-$nt[point_long]))/$x_delta))   +   5; // Shifting in X axis
	$y2= intval((($y_max) *($max_lat-$nt[point_lat]))/$y_delta)   + 5; // Coordinate of Y axis


	//ImageString($im,2,$x2,$y2,$nt[month],$graph_color);
	//Line above is to print month names on the graph
	if($first_one=="no"){ // this is to prevent from starting $x1= and $y1=0
	imageline ($im,$x1,$y1,$x2,$y2,$text_color); // Drawing the line between two points
	//echo "Long=".$nt[point_long]." and Lat=".$nt[point_lat]."<BR>";
	//echo "Line from ".$x1.",".$y1." to ".$x2.",".$y2."<P>";
	}
	else{
		imagefilledellipse($im, $x2, $y2, 10, 10, $start_color);
		//imagestring($im,5,$x2,$y2,"Start",$start_color);
	}
	$x1=$x2; // Storing the value for next draw
	$y1=$y2;
	$first_one="no"; // Now flag is set to allow the drawing
	}
	imagefilledellipse($im, $x2, $y2, 10, 10, $end_color);

/*
$sql="select lat,lng from curr_lat_long LIMIT 1";
if ( ($result = query_wrapper($sql, __FILE__, __LINE__))==null){
	$error_occured = true;//used to check display of remaining body
	echo "Some error occured!";
}
else{
	while($nt=mysql_fetch_array($result)){

	//echo "$nt[month], $nt[sales]";
	$x2=-(intval((($x_max) *(76.489594306856-$nt["lng"]))/$x_delta))   +   5; // Shifting in X axis
	$y2= intval((($y_max) *(9.0998068175776-$nt["lat"]))/$y_delta)   + 5; // Coordinate of Y axis
	}
	imagefilledellipse($im, $x2, $y2, 10, 10, $mid_color);
}*/
ImagePNG ($im);
}
?>